package ruifight.member.wxController;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonNode;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import ruifight.member.model.UserInfo;
import ruifight.member.model.UserInfoExample;
import ruifight.member.service.UserInfoService;
import ruifight.member.util.CommonUtil;
import ruifight.member.util.HttpClientUtil;
import ruifight.member.util.JsonUtils;

@Controller
@RequestMapping("interface/wx/openId")
public class OpenIdController {

	private static final Logger logger = LoggerFactory.getLogger(OpenIdController.class);
	
	@Resource
	private UserInfoService userInfoService;
	
	/**
	 * 获取openId
	 * @return
	 */
	@RequestMapping("/getOpenId/{js_code}")
	@ResponseBody
	public Map<String, Object> getOpenId(@PathVariable String js_code) {
		Map<String, Object> map = new HashMap<String, Object>();
		String getOpenIdUrl=CommonUtil.getPropertiesParam("getOpenIdUrl");
		String appid=CommonUtil.getPropertiesParam("appid");
		String secret=CommonUtil.getPropertiesParam("secret");
		String grant_type=CommonUtil.getPropertiesParam("grant_type");
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("appid", appid);
			paramMap.put("secret", secret);
			paramMap.put("grant_type", grant_type);
			paramMap.put("js_code", js_code);
			String msg=HttpClientUtil.doGet(getOpenIdUrl, paramMap);
			JsonNode jsonMsg=JsonUtils.fromJson(msg);
			JsonNode errcode=jsonMsg.get("errcode");
			if(errcode==null||errcode.asInt()==0) {
				String openid=jsonMsg.get("openid").asText();
				map.put("openid", openid);
				map.put("errcode", 0);
			}else {
				map.put("errcode", errcode.asInt());
			}
		}catch (Exception e){
			map.put("errcode", -1);
			logger.error("获取openId失败", e);
		}
		
		return map;
	}
	/**
	 * 验证openId
	 * @return
	 */
	@RequestMapping("/testOpenId/{openId}")
	@ResponseBody
	public Map<String, Object> testOpenId(@PathVariable String openId) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			UserInfoExample userInfoExample=new UserInfoExample();
			userInfoExample.createCriteria()
				.andOpenIdEqualTo(openId)
				.andUserStateEqualTo(1);
			UserInfo userInfo=userInfoService.selectOneByExample(userInfoExample);
			if(userInfo==null) {
				map.put("errcode", -1);
			}else {
				map.put("errcode", 0);
				map.put("userInfo", userInfo);
				Map<String, Object> claims=new HashMap<String, Object>();
		    	claims.put("loginName", userInfo.getLoginName());
		    	claims.put("password", userInfo.getPassword());
				String token = Jwts.builder()
			                .setClaims(claims)
//			                .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 24 * 1000))
			                .signWith(SignatureAlgorithm.HS512, "ruifight")
			                .compact();
				map.put("token", "Bearer " + token);
			}
		}catch (Exception e){
			map.put("errcode", -1);
			logger.error("验证openId失败", e);
		}
		return map;
	}
}
